package com.dragee.modules.work.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dragee.common.PageUtils;
import com.dragee.common.R;
import com.dragee.config.exception.BusinessException;
import com.dragee.modules.work.entity.ShopEntity;
import com.dragee.modules.work.entity.ShopInoutDetailEntity;
import com.dragee.modules.work.entity.ShopInoutEntity;
import com.dragee.modules.work.service.ShopGoodsService;
import com.dragee.modules.work.service.ShopInoutDetailService;
import com.dragee.modules.work.service.ShopInoutService;
import com.dragee.modules.work.service.ShopService;
import com.dragee.utils.LoginUserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;



/**
 * 出入库
 *
 * @author tc
 * @email tc@mail.com
 * @date 2023-05-24 11:27:57
 */
@RestController
@RequestMapping("work/shopinout")
@Api(tags = "出入库记录")
public class ShopInoutController {
    @Autowired
    private ShopInoutService shopInoutService;
    @Autowired
    private ShopService shopService;
    @Autowired
    private ShopInoutDetailService shopInoutDetailService;
    @Autowired
    private ShopGoodsService shopGoodsService;
    /**
     * 列表
     */
    @GetMapping("list")
    @ApiOperation("出入库列表")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = shopInoutService.queryPage(params);
        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @GetMapping("{id}")
    @ApiOperation("出入库信息")
    public R info(@PathVariable("id") Long id){
		ShopInoutEntity shopInout = shopInoutService.getInfo(id.toString());
        return R.ok().put("data", shopInout);
    }

    /**
     * 保存
     */
    @PostMapping("saveOrUpdate")
    @ApiOperation("保存或修改出入库")
    public R saveOrUpdate(@RequestBody ShopInoutEntity shopInout){
        ShopEntity shop = shopService.getShopByUserId(LoginUserUtil.getLoginUserId().toString());
        if (shop == null) {
            throw new BusinessException("当前用户没有店铺，不能操作");
        }
        shopInout.setShopId(shop.getId().toString());
        shopInoutService.saveOrUpdate(shopInout);
        List<ShopInoutDetailEntity> details = shopInout.getDetails();
        for (ShopInoutDetailEntity detail : details) {
            detail.setInOutId(shopInout.getId().toString());
            detail.setShopId(shop.getId().toString());
            shopInoutDetailService.saveOrUpdate(detail);

            // 修改库存
            if (shopInout.getType().equals("入库")) {
                shopGoodsService.addKunCun(shop.getId().toString(), detail.getGoodId(), detail.getTotal());
            } else if (shopInout.getType().equals("出库")) {
                shopGoodsService.removeKunCun(shop.getId().toString(), detail.getGoodId(), detail.getTotal());
            }
        }
        return R.ok();
    }


    /**
     * 删除
     */
    @GetMapping("delete-by-id")
    @ApiOperation("出入库删除")
    public R delete( String id){
		shopInoutService.removeById(Long.parseLong(id));
        shopInoutDetailService.remove(new QueryWrapper<ShopInoutDetailEntity>().eq("inOutId", id));
        return R.ok();
    }

    /**
     * 删除
     */
    @GetMapping("details/delete-by-id")
    @ApiOperation("出入库明细删除")
    public R detailsDelete( String id){
        shopInoutDetailService.removeById(id);
        return R.ok();
    }

    @ApiOperation(value = "数据导入")
    @PostMapping("/import")
    public R importData(@RequestParam("file") MultipartFile file) throws IOException {
        return shopInoutService.importData(file);
    }

    @ApiOperation(value = "数据导出")
    @GetMapping("/export")
    public void exportData(@RequestParam Map<String, Object> params, HttpServletResponse response ) throws Exception {
            shopInoutService.exportData(params, response);
    }

}
